import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import './css/iconfont.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
import routes from './routes'
import axios from 'axios'
/*import Mock from './mock'
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'

//配置axios的全局基本路径
axios.defaults.baseURL= '/api',
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
    Vue.prototype.$http = axios

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });
axios.interceptors.request.use(config => {
    if (sessionStorage.getItem('uToken')) {
        // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
        //config.headers.put("U-TOKEN",sessionStorage.getItem('uToken'));
        config.headers['U-TOKEN']=sessionStorage.getItem('uToken');
    }
    return config
}, error => {
    // Do something with request error
    Promise.reject(error)
})
axios.interceptors.response.use(config =>{
    let data = config.data;
    if (!data.success&&data.message==="noUser"){
        // console.log("noUser");
        router.push('/login')
        // router.push({name:'login'});
        // router.push({path:'/Login'})
    }
    return config;
}),error=>{
    Promise.reject(error)
}
const router = new VueRouter({
    routes
})
//路由跳转拦截
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login') {
    sessionStorage.removeItem('user');
  }

  let user = JSON.parse(sessionStorage.getItem('user'));
  if (!user && to.path != '/login') {
    next({ path: '/login' })
  } else {
      if (to.path != '/' && to.path != '/login') {
          let flag = false;
          let permissionStr = sessionStorage.getItem('permissionList');
          let permissionList = JSON.parse(permissionStr);
          for (let i = 0; i<permissionList.length;i++){
              // console.log(permissionList[i].url + '==' + to.path)
             if (permissionList[i].url == to.path){
                 // console.log(permissionList[i] + '==' + to.path)
                 flag = true;
             }
          }
          if (flag){
              next()
          } else {
              console.log("没有权限")
              next({ path: from
                      .path })
          }
      }else {
          next()
      }

  }
})

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
    //el: '#app',
    //template: '<App/>',
    router,
    store,
    //components: { App }
    render: h => h(App)
}).$mount('#app')



//自定义颗粒度显示按钮等
Vue.directive('hasAuthorization',{
    bind:(el)=>{
        const roles=localStorage.getItem("roles");
        console.log(roles);
        if(!(localStorage.getItem('roles').indexOf('admin')>-1)){
            el.setAttribute('style','display:none')
        }
    }
})

